using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Net;
using System.Text.RegularExpressions;
using Middleware; //interfaces - client server

namespace Middleware
{
    public class MiddlewareClient : IClient_cmd, IClient_rpt
    {
        //INetwork_cmd mNetwork;
        IClient_rpt mClient;
        Socket.SocketClient mSocketClient;

        public MiddlewareClient(IClient_rpt a_client)
        {
            //mNetwork = new NetworkClient(this);            
            mSocketClient = new Socket.SocketClient(ReadSocketData);
            mClient = a_client;
        }

        public void ReadSocketData(byte[] stream)
        {
            Console.WriteLine("[MC]::ReadSocketData");
        }

        ///////////////////////METHODS CALLED BY CLIENT//////////////////////////
        #region IClient_cmd Members

        public void connect(String a_ServerIP)
        {
            Console.WriteLine("[MC]::CONNECT from CLI SIP:{0}", a_ServerIP);

            //Get local ips
            IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

            Regex regEx = new Regex("192.168.1.\\d{1,3}"); //TODO: ADDAPT
            //Find proper one like 10.28.1.4
            String foundIP = "";
            foreach(IPAddress ipAddress in localIPs)
            {
                Console.WriteLine("[MC]::CONNECT myIP::{0}", ipAddress.ToString());
                if (regEx.IsMatch(ipAddress.ToString()))
                {
                    foundIP = ipAddress.ToString();
                    break;
                }
            }

            //Send info to network layer
            Console.WriteLine("[MC]::CONNECT sending from {0}", foundIP);
            mSocketClient.Connect(a_ServerIP, 8000);

            //Pchanie do kostiego, musze znac ipka swojego i dac to w paramie
            mClient.connectResult(eReturnErrorCode.Success);
        }

        public void ready(DataTable a_dtShipsTable)
        {
            Console.WriteLine("[MC]::READY from CLI");
            //PRZEPYCHAM SHIPS W STREAMIE
        }

        public void shoot(uint a_uiXPos, uint a_uiYPos)
        {
            Console.WriteLine("[MC]::SHOOT from CLI X:{0},Y:{1}", a_uiXPos, a_uiYPos);
            //mNetwork.sendStream();
        }

        public void disconnect()
        {
            Console.WriteLine("[MC]::DISCONNECT from CLI");
            //Notyfikuje dalej ze klient sie rozlaczyl
        }
        #endregion

        #region IClient_rpt Members

        public void readyResult(eReturnErrorCode a_eRetCode)
        {
            Console.WriteLine("[MC]::READY RESULT from CLI");
        }

        public void shootResult(uint a_uiXPos, uint a_uiYPos, uint a_uiShooted)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void disconnectResult(eReturnErrorCode a_eRetCode)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void connectResult(eReturnErrorCode a_eRetCode)
        {
            Console.WriteLine("[MC]::CONNECT RESULT");
            mClient.connectResult(a_eRetCode);
        }

        #endregion
    }

    /*
    public class MiddlewareServer : IServer_cmd, IServer_rpt
    {
        //INetwork_cmd mNetwork;
        IServer_rpt mServer;

        public MiddlewareServer(IServer_rpt a_server)
        {
            //mNetwork = new NetworkServer(this);
            mServer = a_server;
        }

        ///////////////////////METHODS CALLED BY SERVER/////////////////////////
        #region IServer_cmd Members

        public void readyResult(uint a_uiClientID, eReturnErrorCode a_eRetCode)
        {
            Console.WriteLine("READY RESULT from SRV CID:{0},EC:{1}", a_uiClientID, a_eRetCode);
        }

        public void shootPerformed(uint a_uiClientID, uint a_uiXPos, uint a_uiYPos, uint a_uiShooted)
        {
            Console.WriteLine("SHOOT PERFORMED by SRV CID:{0},X:{1},Y:{2},HIT:{3}", a_uiClientID, a_uiXPos, a_uiYPos, a_uiShooted);
        }

        public void disconnect(eReturnErrorCode a_eRetCode)
        {
            Console.WriteLine("DISCONNECT from SRV EC:{0}", a_eRetCode);
        }

        public void connect(eReturnErrorCode a_eRetCode)
        {
            Console.WriteLine("CONNECT from SRV EC:{0}", a_eRetCode);
        }

        #endregion
    
        #region IServer_rpt Members

        public void  connect(uint uiClientID)
        {
 	        throw new Exception("The method or operation is not implemented.");
        }

        public void  ready(uint a_uiClientID, DataTable a_dtShipsTable)
        {
 	        throw new Exception("The method or operation is not implemented.");
        }

        public void  shoot(uint a_uiClientID, uint a_uiXPos, uint a_uiYPos)
        {
 	        throw new Exception("The method or operation is not implemented.");
        }

        public void  disconnect(uint uiClientID)
        {
 	        throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
     */
}
